package com.example.gustavo.hello.activity;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.gustavo.hello.R;
import com.example.gustavo.hello.model.RealmObj;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;

import io.realm.Realm;
import io.realm.RealmBaseAdapter;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.RealmObject;

public class RealmActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_realm);

        TextView tv = (TextView) findViewById(R.id.tv);


        Typeface fromAsset = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
        tv.setTypeface(fromAsset);

        RealmConfiguration build = new RealmConfiguration.Builder(getApplicationContext())
                .migration(new RealmMigration() {
                    @Override
                    public long execute(Realm realm, long version) {
                        return 0;
                    }
                })
                .build();
        Realm.setDefaultConfiguration(build);

    }

    public void click(View view){

        Button button = (Button) view;
        button.setText("haha");

        Realm realm = Realm.getDefaultInstance();

        RealmObj realmObj = new RealmObj();
        realmObj.setInfo("ok");
        realmObj.setData("hiahia");

        realm.beginTransaction();
        RealmObj realmObj1 = realm.copyToRealm(realmObj);
        realm.commitTransaction();

        Gson gson = new GsonBuilder()
                .setExclusionStrategies(new ExclusionStrategy() {
                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        return f.getDeclaredClass().equals(RealmObject.class);
                    }

                    @Override
                    public boolean shouldSkipClass(Class<?> clazz) {
                        return false;
                    }
                })
                .create();

    }
}
